package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanFirstProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanSecondProcessConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanFirstProcessModule;
import com.yanqu.road.server.gameplayer.module.activity.zhenhuan.ZhenHuanSecondProcessModule;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.math.BigInteger;

public class UseRecoverZhenHuanPower extends BaseUseGoods {

    public UseRecoverZhenHuanPower(GamePlayer player, GoodsInfo goodsInfo, long param) {
        super(player, goodsInfo, param);
    }

    @Override
    int checkUse(long count) {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.ZhenHuanFirstProcess.getValue())){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        UseGoodsResult useGoodsResult = new UseGoodsResult();
        useGoodsResult.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);

        ZhenHuanFirstProcessConfig firstProcessConfig = ZhenHuanConfigMgr.getFirstProcessConfig();
        if (firstProcessConfig != null) {
            if (goodsInfo.getGoodsId() == firstProcessConfig.getPowerGoodsId()){
                int powerAdd = goodsInfo.getParamList().get(0).multiply(BigInteger.valueOf(count)).intValue();
                if (powerAdd == 0) {
                    useGoodsResult.setRet(GameErrorCode.E_BAG_USE_GOODS_LIMIT);
                    return useGoodsResult;
                }

                useGoodsResult.setRet(0);
                player.getModule(ZhenHuanFirstProcessModule.class).addPower(powerAdd);
            }
        }

        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (secondProcessConfig != null) {
            if (goodsInfo.getGoodsId() == secondProcessConfig.getPowerGoodsId()) {
                int powerAdd = goodsInfo.getParamList().get(0).multiply(BigInteger.valueOf(count)).intValue();
                if (powerAdd == 0) {
                    useGoodsResult.setRet(GameErrorCode.E_BAG_USE_GOODS_LIMIT);
                    return useGoodsResult;
                }

                useGoodsResult.setRet(0);
                player.getModule(ZhenHuanSecondProcessModule.class).addPower(powerAdd);
            }
        }

        return useGoodsResult;
    }
}
